// 提交登录按钮
import { postUserLoginHttp, putUserIsLoginHttp } from '../../../request/userHttp'
const userLoginData = []
const userData = ref({})
export const useLoginHttp = async (formRef, form) => {
  await formRef.value.validate((valid) => {
    if (valid) {
      postUserLoginHttp(form.username, form.password).then((response) => {
        const { code } = response.data
        if (code === 0) {
          ElMessage({
            message: '登录成功.',
            type: 'success'
          })
          const { id, user_name, user_role } = response.data.result
          userData.value = response.data.result
          userLoginData.push({ id: id, name: user_name, role: user_role })
          localStorage.setItem('userLoginData', JSON.stringify(userLoginData))
          putUserIsLoginHttp(id, 1)
        }
        if (code === 1) {
          ElMessage.error('账号或者密码错误')
        }
      })
    }
  })
  return { userData }
}
